import Variable from "../pages/const/index";
import { ResponseData } from "../pages/types/common";
import { HttpMethod } from "../pages/types/enum";

export default class RequestHttp{
  static get<T>(url:string,params?:object){
    return request<T>(url,HttpMethod.GET,params)
  }
  static post<T>(url:string,data?:object){
    return request<T>(url,HttpMethod.POST,data)
  }
  static put(){}
  static delete(){}
}

function request<T> (url:string,method:HttpMethod,data?:object){
  return new Promise((resolve,reject)=>{
    wx.request<ResponseData<T>>({
      url:Variable.BASE_URL + url,
      method,
      data,
      success(res){
        if(res.data.message === 'OK'){
          resolve(res.data.data)
        }else{
          wx.showToast({
            title:res.data.msg,
            icon:'error'
          })
          reject(new Error(res.data.msg))
        }
      },
      fail(res){
        reject(new Error(res.errMsg))
      }
    })
  })
}